# Copyright (C) 2009  Rob Britton
#
# This file is part of OORegress.
#
# OORegress is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'openoffice'

load_classes "beans.PropertyValue",
  "beans.XPropertySet",
  "sheet.XSpreadsheetDocument",
  "sheet.XSpreadsheetView",
  "sheet.XSpreadsheet",
  "table.XCell",
  "table.CellContentType",
  "frame.XModel",
  "frame.XComponentLoader"

module OpenOffice
  class Calc < OpenOffice::Base
    def initialize(filename = nil)
      super()

      begin
        xComponentLoader = XComponentLoader.query_uno(@desktop)

        loadProps = [].to_java(PropertyValue)

        filename ||= "private:factory/scalc"
        @xSpreadSheetComponent = xComponentLoader.loadComponentFromURL(filename,
                                                                      "_blank", 0, loadProps)

        @xSpreadSheetDocument = XSpreadsheetDocument.query_uno(@xSpreadSheetComponent)

        @xSpreadSheets = @xSpreadSheetDocument.getSheets

        @xSpreadsheetModel = XModel.query_uno(@xSpreadSheetComponent)
        @xSpreadsheetController = @xSpreadsheetModel.getCurrentController
        @xSpreadsheetView = XSpreadsheetView.query_uno(@xSpreadsheetController)

      rescue
        @xRemoteContext = nil
        puts $!
        #puts $!.backtrace
        exit
      end
    end

    def setActiveSheet sheet
      xSheet = XSpreadsheet.query_uno(@xSpreadSheets.getByName(sheet))
      @xSpreadsheetView.setActiveSheet(xSheet)
    end

    def getActiveSheet
      @xSpreadsheetView.getActiveSheet
    end

    def insertSheet name, at = 0
      @xSpreadSheets.insertNewByName name, at
      sheet = XSpreadsheet.query_uno(@xSpreadSheets.getByName(name))
      @xSpreadsheetView.setActiveSheet sheet
      sheet
    end

    def []= x, y, value
      if value.is_a? String
        getActiveSheet.getCellByPosition(x, y).setFormula value
      else
        getActiveSheet.getCellByPosition(x, y).setValue value
      end
    end

    def [] x, y
      cell = getActiveSheet.getCellByPosition(x, y)
      case cell.getType
      when CellContentType::EMPTY
        ""
      when CellContentType::TEXT
        cell.getFormula
      else # formula or value
        cell.getValue
      end
    end

    def formula x, y
      getActiveSheet.getCellByPosition(x, y).getFormula
    end

    def setStyle x, y, style
      XPropertySet.query_uno(getActiveSheet.getCellByPosition(x,y)).setPropertyValue("CellStyle", style)
    end

    def formula x, y
      getActiveSheet.getCellByPosition(x, y).getFormula
    end

    def close
      @xSpreadSheetComponent.dispose
    end
  end
end
